<?php
$sql = mysql_connect("localhost", "sithy", "sithy");
mysql_select_db("symfony");
//
//$res = mysql_query("select * from reason order by id");
//$idList = null;
//while ($reason = mysql_fetch_assoc($res))
//{
//    $str = $reason['name'];
//    preg_match_all('/[^|]+/i', $str, $words);
//    print_r($words);
//    $match = preg_match('/\|h(.+)\]/i', $str, $words2);
//    print_r($words2);
//    if ( count($words[0]) == 5 )
//    {
//        $r = substr($words[0][2], 1);
//        if (substr($r, 0, 1) != "[")
//            $r = "[".$r;
//        if (substr($r, -1, 1) != "]")
//            $r = $r."]";
//        $str = "|".$words[0][0]."|".$words[0][1]."|h".$r."|h|r";
//        $str = str_replace("'", "\\'", $str);
//        echo $str;
//        //echo "update reason set name='".$str."' where id='".$reason['id']."'";
//        mysql_query("update reason set name='".$str."' where id='".$reason['id']."'");
//        echo mysql_error();
//        echo "errno: ".mysql_errno();
//        if (mysql_errno() > 0)
//            $idList[] = $reason['id'];
//    }
//    //echo $str."\n";
//}
//$file = fopen("reasons.txt", "w");
//foreach ($idList as $id)
//    fwrite($file, $id.", ");
//fclose($file);

function getScheduledRaid($dt)
{
    $schedule = array();
    $schedule[0]['begin'] = "00:00";
    $schedule[0]['end'] =   "06:00";
    $schedule[1]['begin'] = 0;
    $schedule[1]['end'] =   0;
    $schedule[2]['begin'] = "00:00";
    $schedule[2]['end'] =   "06:00";
    $schedule[3]['begin'] = 0;
    $schedule[3]['end'] =   0;
    $schedule[4]['begin'] = "00:00";
    $schedule[4]['end'] =   "06:00";
    $schedule[5]['begin'] = 0;
    $schedule[5]['end'] =   0;
    $schedule[6]['begin'] = 0;
    $schedule[6]['end'] =   0;

    $return = null;
    list($date, $time) = explode(" ", $dt);
    $datets = strtotime($date." GMT");
    $ts = strtotime($dt." GMT");
    for ($d = -1; $d < 1; $d++)
    {
        $str = "+1 days";
        if ($d == -1)
            $str = "-1 days";
        $dts = $datets;

        if ($d != 0)
            $dts = strtotime($str, $dts);
        for ($i = 0; $i < count($schedule); $i++)
        {
            if (is_string($schedule[$i]['begin']))
            {
                list($hour, $minute) = explode(":", $schedule[$i]['begin']);
                $begin = $datets + intval($hour)*60*60 + intval($minute)*60;
                list($hour, $minute) = explode(":", $schedule[$i]['end']);
                $end = $datets + intval($hour)*60*60 + intval($minute)*60;
                if ($end <= $begin)
                        $end =   strtotime("+1 day", $end);
                if ($begin <= $ts && $end >= $ts)
                {
                    $return = $date;
                    break;
                    break;
                }
            }
        }
    }
    return $return;
}

$result = mysql_query("select * from history");
while ($row = mysql_fetch_assoc($result))
{
    $sd = getScheduledRaid($row['dt']);
    mysql_query("update history set scheduled_date='".$sd."' where dt='".$row['dt']."'");
    echo mysql_error()."\n";
    echo "update history set scheduled_date='".$sd."' where dt='".$row['dt']."'\n";
    echo $row['dt']." ".$sd."\n";
}

?>
